﻿using System;
using System.Data;
using System.Web.UI;
using Besture.Bll;
using Besture.Common.Helper;
using Besture.Model;
using Besture.UI.ClassHelper;
using DevExpress.Web.ASPxGridView; 

namespace Besture.UI.Admin.Control
{
    public partial class BatchModifyPricesControl : System.Web.UI.UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            //绑定酒店份数据
            DataTable tableProvince = QueryDac.GetTable("select * from TBHotels where IsDeleted=0 order by SortID");
            ControlHelper.BindDropDownList(ddlUpdateHotels, tableProvince, "HotelsName", "ID");
            
            AspxGridViewHelper.InitAspxGridView(grid);                   

        }


        #region 删除数据 getBtnBlue();

        /// <summary>
        /// AspxGridView回发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridCustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            if (e.Parameters == "Refresh")
                grid.DataBind();

        }

        /// <summary>
        /// 删除选中数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LbDelClick(object sender, EventArgs e)
        {
            var ids = AspxGridViewHelper.GetCheckID(grid);
            if (string.IsNullOrEmpty(ids))
            {
                return;
            }
            DataTable dt = (DataTable)ViewState["_dt"];
            dt = dt.Select("ID not in(" + ids + ")").Length > 0 ? dt.Select("ID not in(" + ids + ")").CopyToDataTable() : new DataTable();

            grid.DataSource = dt;
            grid.DataBind();
            ViewState["_dt"] = grid.DataSource;
            //绑定数据后重新注册一下JS事件
            GetRegister();
            
        }


        #endregion



        #region 查询 保存


        public string GetWeekTime(string sWeek)
        {
            string weekstr = "";
            string[] v = sWeek.Split(',');
            foreach (string t in v)
            {
                switch (Convert.ToInt32(t))
                {
                    case 1: weekstr += "周一 "; break;
                    case 2: weekstr += "周二 "; break;
                    case 3: weekstr += "周三 "; break;
                    case 4: weekstr += "周四 "; break;
                    case 5: weekstr += "周五 "; break;
                    case 6: weekstr += "周六 "; break;
                    case 7: weekstr += "周日 "; break;
                    default: weekstr = "全部"; break;
                }
            }
            return weekstr;
        }


        private string GetWeek(DateTime data)
        {
            string weekstr = data.DayOfWeek.ToString();
            switch (weekstr)
            {
                case "Monday":
                    weekstr = "1";
                    break;
                case "Tuesday":
                    weekstr = "2";
                    break;
                case "Wednesday":
                    weekstr = "3";
                    break;
                case "Thursday":
                    weekstr = "4";
                    break;
                case "Friday":
                    weekstr = "5";
                    break;
                case "Saturday":
                    weekstr = "6";
                    break;
                case "Sunday":
                    weekstr = "7";
                    break;
            }
            return weekstr;
        }


        //保存 
        protected void BtnSubmitClick(object sender, EventArgs eventArgs)
        {  
            int lsRecord = 0; 

            decimal decPrice = 1;
            decimal decLowPrice = 1;
            decimal decOneDealerPrice = 1;
            decimal decTwoDealerPrice = 1;
            decimal decThreeDealerPrice = 1;
            int ilBreakfast = 1;

            for (int i = 0; i < grid.VisibleRowCount; i++) // grid 总行数
            {
                string sRoomId = this.grid.GetRowValues(i, "RoomID").ToString();
                //"42,2100.00,78678,567645,34555,56778768,4|"]
                var vRowPrice = hidPrice.Value.TrimEnd('|');
                string[] sRowPrice = vRowPrice.Replace("|,","|").Split('|');
                if (sRowPrice[i].Split(',')[0] == sRoomId)
                {
                    decLowPrice = Convert.ToDecimal(sRowPrice[i].Split(',')[1]);
                    decPrice = Convert.ToDecimal(sRowPrice[i].Split(',')[2]);
                    decOneDealerPrice = Convert.ToDecimal(sRowPrice[i].Split(',')[3]);
                    decTwoDealerPrice = Convert.ToDecimal(sRowPrice[i].Split(',')[4]);
                    decThreeDealerPrice = Convert.ToDecimal(sRowPrice[i].Split(',')[5]);
                    ilBreakfast = Convert.ToInt32(sRowPrice[i].Split(',')[6]);
                } 
                string[] sData = string.Format( this.grid.GetRowValues(i, "RoomTime").ToString()).Split('至');
                TimeSpan iDataCount = Convert.ToDateTime(Convert.ToDateTime(sData[1]).ToString("yyyy/MM/dd")) -
                    Convert.ToDateTime(Convert.ToDateTime(sData[0]).ToString("yyyy/MM/dd"));
                DateTime iData = Convert.ToDateTime(sData[0]);
                for (int j = 0; j < iDataCount.Days+1; j++) // add 条数
                {
                    var vWeek = hidApplicableWeek.Value.TrimEnd(',');
                    if (vWeek == "0")
                    {
                        GetEach(iData, sData, sRoomId, decLowPrice, decPrice, decOneDealerPrice, decTwoDealerPrice, decThreeDealerPrice, ilBreakfast);
                    }
                    else
                    {
                        if (vWeek.Contains(GetWeek(iData.AddDays(j))))
                            GetEach(iData.AddDays(j), sData, sRoomId, decLowPrice, decPrice, decOneDealerPrice, decTwoDealerPrice, decThreeDealerPrice, ilBreakfast);
                    }
                }
            }
            if (lsRecord > 0)
            {
                FunHelper.Alert("保存成功",Request.Url.ToString()); 
            } 
        }


        protected int GetEach(DateTime iData,string[] sData,string sRoomId,decimal decLowPrice, decimal decPrice, decimal decOneDealerPrice, decimal decTwoDealerPrice, decimal decThreeDealerPrice, int ilBreakfast)
        {
            int lsRecord = 0; 
            if (iData <= Convert.ToDateTime(sData[1]))
            {
                RoomPriceModel model = new RoomPriceModel
                {
                    RoomID = Convert.ToInt32(sRoomId),
                    RoomTime = iData, //开始日期 
                    SortID = Convert.ToInt32(SortService.GetSortID("TBRoomPrice", false)),
                    Price = decPrice,
                    LowPrice = decLowPrice,
                    OneDealerPrice = decOneDealerPrice,
                    TwoDealerPrice = decTwoDealerPrice,
                    ThreeDealerPrice = decThreeDealerPrice,
                    IsDeleted = 0,
                    Breakfast = ilBreakfast
                };
                new BaseDac<RoomPriceModel>().LoadAddBaseModel(model);
                lsRecord += new RoomPriceDac().GetModelByRoomIdAndRoomTime(Convert.ToInt32(sRoomId), iData) != null ?
                    new RoomPriceDac().UpdateRoomIdAndRoomTime(model) :
                    new RoomPriceDac().Add(model);
                iData = iData.AddDays(1);
            }
            return lsRecord;
        }


        // 查询
        protected void BtnSeachClick1(object sender, EventArgs e)
        { 
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("RoomID");
            dt.Columns.Add("RoomTime");
            dt.Columns.Add("LowPrice");
            dt.Columns.Add("Price");
            dt.Columns.Add("OneDealerPrice");
            dt.Columns.Add("TwoDealerPrice");
            dt.Columns.Add("ThreeDealerPrice");
            var vRoomTime = hidStartRoomTime.Value + " 至 " + hidEndRoomTime.Value;

            decimal decLowPrice = 1;
            decimal decPrice = 1;
            decimal decOneDealerPrice = 1;
            decimal decTwoDealerPrice = 1;
            decimal decThreeDealerPrice = 1;
            if (hidRoomID.Value.Length > 0)
            {
                string[] sRoomId = hidRoomID.Value.TrimEnd(',').Split(',');
                for (int i = 0; i < sRoomId.Length; i++)
                {
                    //酒店id 和房型id 得到 房型低价 
                    var vRoomModel = new RoomPriceDac().GetModelByRoomId(Convert.ToInt32(sRoomId[i]),1);
                    if (vRoomModel != null)
                    {
                        decLowPrice = vRoomModel.LowPrice; 
                        decPrice= vRoomModel.Price;
                        decOneDealerPrice = vRoomModel.OneDealerPrice;
                        decTwoDealerPrice= vRoomModel.TwoDealerPrice;
                        decThreeDealerPrice = vRoomModel.ThreeDealerPrice;
                    }
                    DataRow newdr = dt.NewRow();
                    newdr["ID"] = i + 1;
                    newdr["RoomID"] = sRoomId[i];
                    newdr["RoomTime"] = vRoomTime;
                    newdr["LowPrice"] = decLowPrice; 
                    newdr["Price"] = decPrice;
                    newdr["OneDealerPrice"] = decOneDealerPrice;
                    newdr["TwoDealerPrice"] = decTwoDealerPrice;
                    newdr["ThreeDealerPrice"] = decThreeDealerPrice;
                    dt.Rows.Add(newdr);
                }
            }
            if (dt.Rows.Count > 0)
            {
                grid.DataSource = dt;
                grid.DataBind();
                ViewState["_dt"] = grid.DataSource;       
            }
            //绑定数据后重新注册一下JS事件
            GetRegister();
        }



        /// <summary>
        /// 绑定数据后重新注册一下JS事件
        /// </summary>
        private void GetRegister()
        {
            //绑定数据后重新注册一下JS事件
            ScriptManager.RegisterClientScriptBlock(this.UpdatePanel3, GetType(), "Hotels", "getUpdateHotels();setTab(this, 1);getBtnBlue();GetInLoadCalendar();GetQuery();GetChecked();", true); 
        }


        #endregion


        #region 初始化  早餐情况 房型 名称

        /// <summary>
        /// 早餐情况
        /// </summary>
        /// <returns></returns>
        public string GetStringBreakfast()
        {
            //早餐情况 绑定
            var vBreakfast = QueryDac.GetTable("SELECT * FROM TBDictionaryType WHERE Type = 5");
            string sHtml = "<select id='slBreakfast' runat='server' >";
            foreach (DataRow dr in vBreakfast.Rows)
            {
                sHtml += string.Format("<option value={0}>{1}</option>", dr["Code"], dr["name"]);
            }
            return (sHtml + "</select>");
        }


        /// <summary>
        /// 房型 名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetRoomNameById(string id)
        {
            string sRoomName = string.Empty;
            var model = new RoomDac().GetModel(Convert.ToInt32(id));
            if (model != null)
            {
                sRoomName = model.RoomName;
            }
            return sRoomName;
        }

        #endregion

    }
}
